package edu.xidian.bdilab.jrbigdata.service.enterprise.checkProductRequest.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.VO.EnterpriseProductRequestVO;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.FinancialProduct;
import edu.xidian.bdilab.jrbigdata.domain.ProductRequest;
import edu.xidian.bdilab.jrbigdata.mapper.FinancialProductMapper;
import edu.xidian.bdilab.jrbigdata.mapper.ProductRequestMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.checkProductRequest.CheckProductRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckProductRequestImpl implements CheckProductRequestService {

    @Autowired
    ProductRequestMapper productRequestMapper;

    @Autowired
    FinancialProductMapper financialProductMapper;

    @Override
    public ResponseResult checkProductRequest(String socialCode){
        ResponseResult responseResult = new ResponseResult();
        Map<String,Object> data=new HashMap<>(1);

        //查出自己企业的所有金融产品申请
        QueryWrapper<ProductRequest> productRequestQueryWrapper = new QueryWrapper<>();
        productRequestQueryWrapper.eq("fk_social_code", socialCode);
        List<ProductRequest> productRequestList = productRequestMapper.selectList(productRequestQueryWrapper);

        ArrayList<EnterpriseProductRequestVO> enterpriseProductRequestVOList = new ArrayList<EnterpriseProductRequestVO>();
        for(ProductRequest productRequest: productRequestList){
            EnterpriseProductRequestVO enterpriseProductRequestVO = new EnterpriseProductRequestVO();
            //通过申请名查询指定产品额度
            FinancialProduct financialProduct = financialProductMapper.selectById(productRequest.getFkProductName());
            //根据获得数据构造返回VO
            enterpriseProductRequestVO.setRequestTime(productRequest.getRequestTime());
            enterpriseProductRequestVO.setFkProductName(productRequest.getFkProductName());
            enterpriseProductRequestVO.setBankName(financialProduct.getBankName());
            enterpriseProductRequestVO.setLoanLimit(financialProduct.getLoanLimit());
            enterpriseProductRequestVO.setRateLowerLimit(financialProduct.getRateLowerLimit());
            enterpriseProductRequestVO.setRateUpperLimit(financialProduct.getRateUpperLimit());
            enterpriseProductRequestVO.setLoanPeriod(financialProduct.getLoanPeriod());
            enterpriseProductRequestVO.setStatus(productRequest.getStatus());
            enterpriseProductRequestVO.setFundUsage(productRequest.getFundUsage());
            //添加到返回列表尾部
            enterpriseProductRequestVOList.add(enterpriseProductRequestVO);
        }

        //构造返回信息
        data.put("enterpriseProductRequestVOList",enterpriseProductRequestVOList);
        responseResult.setData(data);
        responseResult.setMessage("企业 金融产品申请详细信息查询成功");
        responseResult.setSuccess(true);

        return responseResult;
    }
}
